package com.noble.meether.chat.ui.agora.act.video;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.SystemClock;
import android.support.v4.app.FragmentActivity;
import android.support.v7.view.menu.MenuBuilder;
import android.view.View;
import android.widget.Chronometer;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.faceunity.agora.VideoPreProcessing;
import com.noble.glamour.bean.center.user.detail.UserDetail;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.baseui.LoadingDialog;
import com.noble.glamour.module.logic.baseui.popup.OptionMenu;
import com.noble.glamour.module.logic.baseui.popup.OptionMenuView;
import com.noble.glamour.module.logic.baseui.popup.PopupMenuView;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.view.base.BasePanel;
import com.noble.meether.MeetUIShow;
import com.noble.meether.R;
import com.noble.meether.chat.ui.agora.RtcUIShow;
import com.noble.meether.chat.ui.agora.model.EngineConfig;
import com.noble.meether.chat.ui.agora.util.RtcJointChannel;
import com.noble.meether.chat.ui.agora.util.RtcTimerUtil;

/**
 * 视频聊天头部浮层布局
 * Created by Su on 2017/5/31.
 */
public class RtcVideoHeadPanel extends BasePanel implements View.OnClickListener, OptionMenuView.OnOptionMenuClickListener, RequestComplete {
    private boolean openCamera;     // 首次摄像头开关状态
    private ImageView ivCamera, chat_silence, iv_more;    // 切换摄像头
    private PopupMenuView popup;

    private TextView tv_name;

    private EngineConfig config;

    public RtcVideoHeadPanel(Context context, boolean openCamera) {
        super(context);
        setContentView(R.layout.f1_chat_video_header_panel);
        this.openCamera = openCamera;
        initView();
    }

    @SuppressLint("RestrictedApi")
    private void initView() {
        enableFaceU();  // 开启FaceU
        config = ModuleMgr.getRtcEnginMgr().getEngineConfig();
        tv_name = (TextView) findViewById(R.id.tv_name);
        iv_more = (ImageView) findViewById(R.id.iv_more);
        iv_more.setOnClickListener(this);
        findViewById(R.id.iv_add_buddy).setOnClickListener(this);

        chat_silence = (ImageView) findViewById(R.id.chat_silence);
        chat_silence.setSelected(!RtcJointChannel.getInstance().getSwitchAudio());
        chat_silence.setOnClickListener(this);

        ivCamera = (ImageView) findViewById(R.id.btn_switch_camera);
        Chronometer chatCountUp = (Chronometer) findViewById(R.id.ch_count_time);
        chatCountUp.setBase(SystemClock.elapsedRealtime() - config.millisPass);
        RtcTimerUtil.getInstance().addTimer(chatCountUp);
        chatCountUp.start(); // 开启通信计时

        ivCamera.setOnClickListener(this);
        ivCamera.setSelected(!RtcJointChannel.getInstance().getSwitchCamera());
        findViewById(R.id.iv_shrink_narrow).setOnClickListener(this);
        if (!openCamera && ModuleMgr.getCenterMgr().getMyInfo().isMan())
            ivCamera.setVisibility(View.GONE);

        // pop弹框
        popup = new PopupMenuView(getContext(), R.layout.f1_chat_pop_item);
        popup.inflate(R.menu.menu_feedback, new MenuBuilder(getContext()));
        popup.setOrientation(LinearLayout.VERTICAL);
        popup.setOnMenuClickListener(this);
    }

    public void refreshInfo(UserDetail otherInfo) {
        tv_name.setText(otherInfo.getNickname());
    }

    public void refreshCamera(boolean isClose) {
        if (isClose) {
            ivCamera.setVisibility(View.GONE);
            return;
        }
        ivCamera.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chat_silence:// 静音
                switchLocalAudio();
                break;
            case R.id.btn_switch_camera:
                RtcJointChannel.getInstance().switchCamera();
                ivCamera.setSelected(!RtcJointChannel.getInstance().getSwitchCamera());
                break;
            case R.id.iv_shrink_narrow:
                if (listener != null)
                    listener.onPanelClick(v);
                break;
            case R.id.iv_more:  // 投诉
                popup.show(iv_more);
                break;
            case R.id.iv_add_buddy:      // 添加好友
                ModuleMgr.getCommonMgr().getUserRelation(config.mOtherId, response -> MeetUIShow.showAddFriendDlg(config.mOtherId, "", RtcVideoHeadPanel.this));
                break;
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        LoadingDialog.closeLoadingDialog();
    }

    @Override
    public boolean onOptionMenuClick(int position, OptionMenu menu) {
        if (menu.getId() == R.id.user_report) {
            RtcUIShow.showRtcVideoCommentDlg((FragmentActivity) getActivity());
        }

        if (menu.getId() == R.id.user_blacklist) {
            RtcUIShow.showRtcBlackDlg((FragmentActivity) getActivity(), config.mOtherId);
        }
        return true;
    }


    private void switchLocalAudio() {
        RtcJointChannel.getInstance().switchAudio();
        chat_silence.setSelected(!RtcJointChannel.getInstance().getSwitchAudio());
    }

    /**
     * 是否启用FaceU美颜
     */
    private static VideoPreProcessing mVideoPreProcessing;

    private void enableFaceU() {
        if (mVideoPreProcessing == null) {
            mVideoPreProcessing = new VideoPreProcessing();
            mVideoPreProcessing.enablePreProcessing(true);
        }
    }

    // ===================== 回调监听 ==========================
    private PanelListener listener;

    public void setPanelListener(PanelListener listener) {
        this.listener = listener;
    }

    public interface PanelListener {
        void onPanelClick(View v);
    }
}
